package com.y.config.exception;

import com.y.entity.vo.R;
import lombok.extern.slf4j.Slf4j;
//import org.apache.shiro.authc.IncorrectCredentialsException;
//import org.apache.shiro.authc.LockedAccountException;
//import org.apache.shiro.authc.UnknownAccountException;
//import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.exceptions.TemplateInputException;

/**
 * 自定义异常拦截器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 自定义异常
     * @param e
     */
    @ExceptionHandler(BusinessException.class)
    public R businessException(BusinessException e){
        log.info(e.getMessage(),e);
        return R.error().setCode(e.getCode()).setMessage(e.getMessage());
    }

    /**
     * 没有权限异常
     * @param e
     */
//    public ModelAndView authorizationException(Exception e){
//        log.info(e.getMessage(),e);
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("view/error/403");
//        return modelAndView;
//    }


    /**
     * 登录异常
     * @param e 登录异常
     * @return
     */
    @ExceptionHandler({
            AccountLoginException.class
    })
    public ModelAndView loginException(AccountLoginException e){
        ModelAndView modelAndView = new ModelAndView();
        log.info(e.getMessage(),e);
        modelAndView.addObject("msg",e.getMessage());
        modelAndView.setViewName("login");
        return modelAndView;
    }


    @ExceptionHandler(TemplateInputException.class)
    public ModelAndView templateInputException(TemplateInputException e){
        ModelAndView modelAndView = new ModelAndView();
        log.info(e.getMessage(),e);
        modelAndView.setViewName("view/error/500");
        return modelAndView;
    }

}
